{% extends 'base/base_table.html' %}
{% load tags %}

{% block title %}ITAdmin | 批量处理(SlatSaltStack){% endblock %}

{% block css %}
    <link href="/static/css/plugins/datapicker/datepicker3.css" rel="stylesheet">
    <link href="/static/css/plugins/sweetalert/sweetalert.css" rel="stylesheet">
{% endblock %}

{% block table_btn %}
{#<button class="btn btn-sm btn-success" id="add_cmd"><strong>添加命令</strong></button>#}
<div class="btn-group">
    <button data-toggle="dropdown" class="btn btn-primary dropdown-toggle" aria-expanded="false">添加saltJob <span class="caret"></span></button>
    <ul class="dropdown-menu">
        <li><a href="{% url 'addmodel' %}" class="font-bold">salt模块</a></li>
        <li><a href="buttons.html#" class="font-bold">远程命令</a></li>
        <li><a href="{% url 'addshell' %}" class="font-bold">shell脚本</a></li>
        <li><a href="buttons.html#">文件发放</a></li>
    </ul>
</div>
{% endblock %}

{% block table_thead %}
<tr>
    <th style="text-align: center;vertical-align: middle">命令名称</th>
    <th style="text-align: center;vertical-align: middle">命令类型</th>
    <th style="text-align: center;vertical-align: middle">添加时间</th>
    <th style="text-align: center;vertical-align: middle">备注</th>
    <th style="text-align: center;vertical-align: middle">动作</th>
</tr>
{% endblock %}

{% block other %}
<div class="ibox-content m-b-sm border-bottom">
    <div class="row">
        <form action="{{ request.path }}">
        <div class="col-sm-4">
            <div class="form-group">
                <label class="control-label" for="date_added">开始时间</label>
                <div class="input-group date">
                    <span class="input-group-addon"><i class="fa fa-calendar"></i></span><input id="date_added" name="start" type="text" class="form-control" value="{{ start }}">
                </div>
            </div>
        </div>
        <div class="col-sm-4">
            <div class="form-group">
                <label class="control-label" for="date_modified">结束时间</label>
                <div class="input-group date">
                    <span class="input-group-addon"><i class="fa fa-calendar"></i></span><input id="date_modified" name="end" type="text" class="form-control" value="{{ end }}">
                </div>
            </div>
        </div>
        <div class="col-sm-1">
            <div class="form-group">
                <label class="control-label" for="date_modified">时间过滤</label>
                <input type="hidden" value="{{ last_search }}" name="top-search">
                <button class="form-control" type="submit"><strong>确定</strong></button>
            </div>
        </div>
        </form>
    </div>
</div>
{% endblock %}

{% block table_bady %}
{% for object in objects %}
    <tr>
        <td style="text-align: center;vertical-align: middle">
            {% if object.type == 4 %}
            <a href="{% url 'addmodel' %}?id={{ object.id }}">
            {% endif %}
            {% if object.type == 1 %}
            <a href="{% url 'addshell' %}?id={{ object.id }}">
            {% endif %}
                <span class="label label-sm label-success">{{ object.name }}</span>
            </a>
        </td>
        <td style="text-align: center;vertical-align: middle">{{ object.get_type_display }}</td>
        <td style="text-align: center;vertical-align: middle">{{ object.add_time }}</td>
        <td style="text-align: center;vertical-align: middle">
            <div id="doc-content{{ object.id }}">
                <textarea style="display:none;">{{ object.description }}</textarea>
            </div>
        </td>
        <td style="text-align: center;vertical-align: middle">
            <a class="btn btn-sm btn-danger" id="del_{{ object.id }}" name="{{ object.id }}">
                删除<i class="fa fa-bitbucket"></i>
            </a>
            <a class="btn btn-sm btn-primary" href="{% url 'execsalt' %}?cmd_id={{ object.id }}">
                执行<i class="fa fa-paper-plane"></i>
            </a>
        </td>
    </tr>
{% endfor %}
{% endblock %}

{% block base_script %}
    <script src="/static/js/plugins/sweetalert/sweetalert.min.js"></script>
    <script src="/static/js/plugins/datapicker/bootstrap-datepicker.js"></script>
    <script src="/static/md/marked.min.js"></script>
    <script src="/static/md/prettify.min.js"></script>
    <script src="/static/md/raphael.min.js"></script>
    <script src="/static/md/underscore.min.js"></script>
    <script src="/static/md/sequence-diagram.min.js"></script>
    <script src="/static/md/flowchart.min.js"></script>
    <script src="/static/md/jquery.flowchart.min.js"></script>
    <script src="/static/md/editormd.min.js"></script>
{% endblock %}

{% block script %}
<script>

    $(document).ready(function() {

        $('#date_added').datepicker({
            todayBtn: "linked",
            keyboardNavigation: false,
            forceParse: false,
            calendarWeeks: true,
            autoclose: true
        });

        $('#date_modified').datepicker({
            todayBtn: "linked",
            keyboardNavigation: false,
            forceParse: false,
            calendarWeeks: true,
            autoclose: true
        });

    });
</script>
<script type="text/javascript">
    $(function () {

        $('#pages').val('{{ entry }}')
        $('#pages').change(function () {
            var search = '{{ search }}'
            if (search == '') {
                location.href = "{% url 'saltcmd' %}?page={{ num }}&entry=" + $(this).val();
            } else {
                location.href = "{% url 'saltcmd' %}?page={{ num }}&{{ search }}&entry=" + $(this).val();
            }
        });

        $("a[id^='del_']").click(function () {
            var cmd_id = $(this).attr('name')
            swal({
                title: "确定删除?",
                text: "您是否确定要删除该命令!",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                closeOnConfirm: false
                }, function () {
                $.post("{% url 'delcmd' %}", {cmd_id: cmd_id}, function (data) {
                    if (data.status == 1) {
                        swal({title:"成功!", text:"删除命令成功", type:"success"}, function () {
                            location.href = "{% url 'saltcmd' %}";
                        });
                    } else {
                        swal({title:"错误!", text:data.msg, type:"error"}, function () {
                             location.href = "{% url 'saltcmd' %}";
                        });
                    }
                }, 'json');
            });
        });
    });
</script>
<script type="text/javascript">
{% for object in objects %}
var testEditor;
    $(function () {
        testEditor = editormd.markdownToHTML("doc-content{{ object.id }}", {//注意：这里是上面DIV的id
            htmlDecode: "style,script,iframe",
            emoji: true,
            taskList: true,
            tex: true, // 默认不解析
            flowChart: true, // 默认不解析
            sequenceDiagram: true, // 默认不解析
            codeFold: true,
    });});
{% endfor %}
</script>
{% endblock %}